// ==UserScript==
// @name         Sync Form 0.2
// @namespace    http://tampermonkey.net/
// @version      dev 0.2
// @description  try to take over the world!
// @author       You
// @match        https://jira-hpstore.hpcloud.hp.com/browse/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hp.com
// @grant        none
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/jquery-ui.min.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/loadingStyle.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/underscore.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/jsonform.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/jsonform-multiple-select.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/lib/jsonformStyle.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/LoggerJs.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/RequestJs.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/JiraClient.js?v1
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/JiraMapping.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/CustomMapping.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/SyncTickets.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/TicketSyncForm.js
// @require      https://gitee.com/luo3555mail/jira-sync-tools/raw/master/js/FlyBookNotice.js
// @match        http*://*/browse/*-*
// ==/UserScript==
var ticketSyncForm = new TicketSyncForm(JSON.parse(localStorage.getItem('config')));
ticketSyncForm.afterSubmit = function(ticketObject) {
    const regionElement = document.getElementById('customfield_14800-field');
    const regionText = regionElement.textContent || regionElement.innerText;
    // do something after submit
    let flyBook = new FlyBookNotice(ticketObject, regionText?.trim(), JSON.parse(localStorage.getItem('config')));
    flyBook.send();
};

ticketSyncForm.userList = [
    "Name"
];

// allow sync action field
ticketSyncForm.exceptList = [
    "Name"
];

var titleMap = {
                "email": "name"
            };

ticketSyncForm.formSchema = {
    "schema": {
        "fields": {
            "type": "object",
            "title": "Fields",
            "properties": {
                "customfield_13408": {
                    "type":"object",
                    "properties": {
                        "value": {
                            "type": "string",
                            "title": "Effort Size",
                            "enum": [
                                "",
                                "XXS<2hours",
                                "XXS<=2hours",
                                "XS<4hous",
                                "XS<=4hous",
                                "S<1day",
                                "S<=1day",
                                "S < 3 man days",
                                "M<=3days"
                            ],
                            "default": jQuery("#customfield_13408-val").text().trim()
                        }
                    }
                },
                "timetracking": {
                    "type": "object",
                    "properties": {
                        "originalEstimate": {
                            "type": "string",
                            "title": "Original Estimate",
                            "default": jQuery('#tt_single_values_orig').text().trim()
                        }
                    }
                },
                "customfield_13431": {
                    "type": "object",
                    "properties": {
                        "value": {
                            "type": "string",
                            "title": "Ticket Action",
                            "enum": [
                                "None",
                                "Code change",
                                "Update information",
                                "Verification",
                                "Update status",
                                "Configuration",
                                "Export data",
                                "Import data",
                                "Refresh index",
                                "Others"
                            ],
                            "default": jQuery('#customfield_13431-val').text().trim()
                        }
                    }
                },
                "customfield_15300": {
                    "type": "object",
                    "properties": {
                        "value": {
                            "type": "string",
                            "title": "Area Impact",
                            "enum": [
                                "None",
                                "FE - Order",
                                "BE - Order",
                                "FE - Quote",
                                "BE - Quote",
                                "FE - Payment",
                                "BE - Payment",
                                "FE - Invoice",
                                "BE - Invoice",
                                "FE - Shipping",
                                "BE - Shipping",
                                "FE - Delivery",
                                "BE - Delivery",
                                "FE - Cart",
                                "BE - Cart",
                                "FE - Stock",
                                "BE - Stock",
                                "FE - Checkout",
                                "BE - Checkout",
                                "FE - Pincode / Postcode",
                                "BE - Pincode / Postcode",
                                "FE - Category",
                                "BE - Category",
                                "FE - Homepage",
                                "FE - HP Money",
                                "FE - CMS",
                                "FE - PDP",
                                "FE - PLP",
                                "FE - Customer",
                                "BE - Customer",
                                "FE - Account",
                                "BE - Account",
                                "BE - Feed",
                                "FE - Tagging",
                                "BE - Tagging",
                                "FE - UX",
                                "BE - UX",
                                "FE - Carepack",
                                "FE - SEO",
                                "FE - Search",
                                "FE - SMS",
                                "BE - SMS",
                                "FE - Email",
                                "BE - Email",
                                "FE - URL",
                                "BE - URL",
                                "BE - ISR",
                                "BE - API",
                                "BE - Report",
                                "BE - Webhook",
                                "BE - RPL/GTS",
                                "FE - Coupon",
                                "FE - Subscription",
                                "FE - BazaarVoice"
                            ],
                            "default":  jQuery('#customfield_15300-val').text().trim()
                        }
                    }
                },
                "customfield_14800": {
                    "title": "Regions",
                    "type": "string",
                    "enum": [
                        "None",
                        "AP",
                        "LATAM"
                    ],
                    "default": jQuery("#customfield_14800-field").text().trim()
                },
                "customfield_13423": {
                    "title": "Country Covered",
                    "type": "string",
                    "enum": [
                        "None",
                        "AR",
                        "AU",
                        "BR",
                        "CL",
                        "CN",
                        "CO",
                        "HK",
                        "IN",
                        "ID",
                        "KR",
                        "MX",
                        "MY",
                        "NZ",
                        "PE",
                        "SG",
                        "TH",
                        "ALL",
                        "AP",
                        "LAM"
                    ],
                    "default": jQuery("#customfield_13423-field").text().trim().split()
                },
                "customfield_13700": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "title": "Zone",
                            "type": "string",
                            "enum": Object.keys(titleMap),
                            "default": jQuery('#customfield_13700-field .user-hover').attr('rel')
                        }
                    }
                },
                "developer": {
                    "title": "Developer",
                    "type": "string"
                },
                "comment": {
                    "title": "Comment",
                    "type": "textarea",
                    "description": "Solution"
                }
            }
        }
    },
    "form": [
        {
            "key": "fields.customfield_13700.name",
            "titleMap": titleMap
        },
        {
            "key": "fields.developer"
        },
        {
            "key": "fields.customfield_13431"
        },
        {
            "key": "fields.customfield_13408"
        },
        {
            "key": "fields.timetracking.originalEstimate"
        },
        {
            "key": "fields.customfield_15300"
        },
        {
            "type": "multipleSelect",
            "key": "fields.customfield_14800"
        },
        {
            "type": "multipleSelect",
            "key": "fields.customfield_13423"
        },
        {
            "key": "fields.comment"
        }
    ]
};
